import { Component, OnInit } from "@angular/core";
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: "sa-purchase-detail",
	templateUrl: "./purchase-detail.component.html"
})
export class PurchaseDetailComponent implements OnInit {
	tableData: any = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	searchKey: string = ""; //搜索keyword
	buyApplyType: string = '' //申请类型
	materialNumber: string = ""; //查询物料编码
	materialName: string = ""; //查询物料名称
	deliveryAddress: string = ""; //查询交货地址
	applyDepartmet: string = ""; //查询申请部门
	applicant: string = ""; //查询申请人

	jumpData: any; // 跳转页面接收到的数据
	purchaseId: any = ''; // 从订单中跳过来带的维修记录id

	startTime_search: string = '';
	endTime_search: string = '';
	specifyId = {
		picker1: 'datetimepicker1',
		picker2: 'datetimepicker2'
	}
	specifyTime = {
		startTime: '',
		endTime: '',
	}


	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe((queryParams) => {
			if (common.getCookie("purchaseDetail")) {
				this.jumpData = JSON.parse(common.getCookie("purchaseDetail"));
				common.clearCookie("purchaseDetail");
			}
		});
	}

	ngOnInit() {
		// 初始化用户
		var that = this;
		if (that.jumpData) {
			that.purchaseId = that.jumpData.id;
			that.buyApplyType = that.jumpData.buyApplyType;
			that.applyDepartmet = that.jumpData.applyDepartmet;
			that.applicant = that.jumpData.applicant;
			// that.materialNumber = that.jumpData.materialNumber;
			// this.searchKey = this.jumpData.deviceId
		}
		that.load();
	}

	// 接收子组件传递过来的数据 
	getSelectTableTime(time) {
		this.startTime_search = time.startTime;
		this.endTime_search = time.endTime;
	}

	//table刷新
	refresh() {
		this.load();
	}
	//表格搜索
	table_search() {
		this.curPage = 1;
		this.searchKey = common.stringTrim(this.searchKey);
		// 点击搜索的时候清除维修订单id  不然条件搜索都是这个id
		this.purchaseId = ""
		this.load();
	}

	//初始化加载list数据  TODO
	load() {
		var that = this;

		const data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				// searchKey: this.searchKey.trim(),
				startTime: this.startTime_search,
				endTime: this.endTime_search,
				buyApplyId: this.purchaseId,
				materialNumber: this.materialNumber, //查询物料编码
				materialName: this.materialName,//查询物料名称
				deliveryAddress: this.deliveryAddress, //查询交货地址
				applyDepartmet: this.applyDepartmet, //查询申请部门
				applicant: this.applicant, //查询申请人
				buyApplyType: this.buyApplyType
			},
		};
		const reqdata = "gridPager=" + JSON.stringify(data);
		this.jsonApiService.http("dtsBuyApplyDetails/page.html", reqdata).subscribe((result) => {
			if (result.isSuccess) {
				this.tableData = result.exhibitDatas || [];
				this.totalCount = result.recordCount;
			}
		});
	}
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	// 数据导出
	exportData() {
		var params = {
			"pageSize": this.pageSize,
			"nowPage": this.curPage,
			"parameters": {
				// searchKey: this.searchKey.trim(),
				startTime: this.startTime_search,
				endTime: this.endTime_search,
				purchaseId: this.purchaseId,
				materialNumber: this.materialNumber, //查询物料编码
				materialName: this.materialName,//查询物料名称
				deliveryAddress: this.deliveryAddress, //查询交货地址
				applyDepartmet: this.applyDepartmet, //查询申请部门
				applicant: this.applicant, //查询申请人
				buyApplyType: this.buyApplyType,
				export: '1'
			}
		};
		common.exprotDataFile(params, 'gridPager', config.BaseURL + 'dtsBuyApplyDetails/page.html')
	}

	// 删除输入
	delKey(type) {
		if (type == "materialNumber") {
			this.materialNumber = ""
		}
		if (type == "materialName") {
			this.materialName = ""
		}
		if (type == "deliveryAddress") {
			this.deliveryAddress = ""
		}
		if (type == "applyDepartmet") {
			this.applyDepartmet = ""
		}
		if (type == "applicant") {
			this.applicant = ""
		}
	}
}
